package com.myapplication.zhou.alldemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.myapplication.callback.VerifyNumberCallback;
import com.myapplication.dialog.MyDialog;
import com.myapplication.interfaces.VerfiyNumberInter;
import com.myapplication.interfaces.VertifyNumberHttpConnect;

import org.json.JSONException;
import org.json.JSONObject;

public class ForgetPasswordActivity extends AppCompatActivity implements VerifyNumberCallback, View.OnClickListener {

    private TextView textView_title;
    private Toolbar toolbar;
    private VerfiyNumberInter verfiyNumberInter;
    private EditText editText_phone;
    private EditText editText_verify_number;
    private Button button_commit, button_verify;
    private String auth_code;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        init();
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        textView_title.setText("找回密码");
        verfiyNumberInter = new VertifyNumberHttpConnect();
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        button_commit.setOnClickListener(this);
        button_verify.setOnClickListener(this);
    }

    private void init() {
        textView_title = (TextView) findViewById(R.id.textView_title);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        editText_phone = (EditText) findViewById(R.id.editText_phone);
        editText_verify_number = (EditText) findViewById(R.id.edit_verify_number);
        button_commit = (Button) findViewById(R.id.button_commit);
        button_verify = (Button) findViewById(R.id.button_verify);
    }

    /**
     * 判断输入框中是否为手机号
     *
     * @return boolean
     */
    private boolean checkNullableforphone() {
        phone = editText_phone.getText().toString().trim();
        boolean b = false;
        if (phone.length() == 0) {
            new MyDialog().tipDialogByTextAndTimeAndImage(this, "手机号不能为空！", 1500, R.mipmap.ic_cancel_white_24dp).show();
        } else if (phone.length() != 11) {
            new MyDialog().tipDialogByTextAndTimeAndImage(this, "对不起！您输入的手机号长度不正确。", 1500, R.mipmap.ic_cancel_white_24dp).show();

        } else
            b = true;
        return b;

    }

    private boolean checkNullable() {
        String verNumber = editText_verify_number.getText().toString().trim();
        boolean b = false;
        boolean a = checkNullableforphone();
        if (a&&verNumber.length()!=0) {
            b = true;
        } else if (verNumber.length() == 0){
            b = false;
            new MyDialog().tipDialogByTextAndTimeAndImage(this, "验证码不能为空", 1500, R.mipmap.ic_cancel_white_24dp).show();

        }
            return b;
    }

    public void startTimeClick() {
        new MyCount(60 * 1000, 1000).start();

    }

    class MyCount extends CountDownTimer {
        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            button_verify.setFocusable(true);
            button_verify.setClickable(false);
            button_verify.setText(millisUntilFinished / 1000 + "秒");
            button_verify.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
        }

        @Override
        public void onFinish() {
            button_verify.setClickable(true);
            button_verify.clearFocus();
            button_verify.setText("获取验证码");
        }
    }

    /**
     * 获取验证码成功接口
     *
     * @param success {@link VerifyNumberCallback#onVerifyNumberCallbackSuccess(String)}
     */
    @Override
    public void onVerifyNumberCallbackSuccess(String success) {
        Log.e("Verifynumber", success);
        try {
            JSONObject jsonObject = new JSONObject(success);
            int code = jsonObject.optInt("code");
            if (code == 200) {
                JSONObject jsonObject1 = jsonObject.optJSONObject("datas");
                auth_code = jsonObject1.optString("auth_code");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
       /* Snackbar snackbar = Snackbar.make(textView_title, Html.fromHtml("<p font=\"bold\" color = \"red\">错误</p>"),Snackbar.LENGTH_SHORT);
        snackbar.show();*/
    }

    /**
     * 获取验证码失败接口
     *
     * @param fail {@link VerifyNumberCallback#onVerifyNumberCallbackFail(String)}
     */
    @Override
    public void onVerifyNumberCallbackFail(String fail) {
        Toast.makeText(ForgetPasswordActivity.this, "错误", Toast.LENGTH_SHORT).show();
    }

    /**
     * 点击事件
     *
     * @param v {@link android.view.View.OnClickListener}
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_commit:
             /*   button_commit.setFocusable(true);
                button_commit.setFocusableInTouchMode(true);
                button_commit.requestFocus();*/
                if(checkNullable()&&editText_verify_number.getText().toString().trim().equals(auth_code)) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString("phonenumber",phone);
                    bundle.putString("auth_code",auth_code);
                    intent.putExtras(bundle);
                    intent.setClass(this,ChangePasswordActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.button_verify:
                if (checkNullableforphone()) {
                    verfiyNumberInter.getVerfiyNumber(editText_phone.getText().toString().trim(), this);
                    startTimeClick();
                }
                break;
        }
    }
}
